home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / include / CEGUI / renderers / OpenGLGUIRenderer / openglrenderer.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-11-21  |  11.0 KB  |  381 lines

  1. /************************************************************************
  2.     filename:    openglrenderer.h
  3.     created:    9/4/2004
  4.     author:        Mark Strom
  5.                 mwstrom@gmail.com
  6.  
  7.     purpose:    Interface to Renderer implemented via Opengl
  8. *************************************************************************/
  9. /*************************************************************************
  10.     Crazy Eddie's GUI System (http://www.cegui.org.uk)
  11.     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
  12.  
  13.     This library is free software; you can redistribute it and/or
  14.     modify it under the terms of the GNU Lesser General Public
  15.     License as published by the Free Software Foundation; either
  16.     version 2.1 of the License, or (at your option) any later version.
  17.  
  18.     This library is distributed in the hope that it will be useful,
  19.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21.     Lesser General Public License for more details.
  22.  
  23.     You should have received a copy of the GNU Lesser General Public
  24.     License along with this library; if not, write to the Free Software
  25.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  26. *************************************************************************/
  27. #ifndef _openglrenderer_h_
  28. #define _openglrenderer_h_
  29.  
  30.  
  31. #if defined( __WIN32__ ) || defined( _WIN32 )
  32. #   ifdef OPENGL_GUIRENDERER_EXPORTS
  33. #       define OPENGL_GUIRENDERER_API __declspec(dllexport)
  34. #   else
  35. #       define OPENGL_GUIRENDERER_API __declspec(dllimport)
  36. #   endif
  37. #else
  38. #   define OPENGL_GUIRENDERER_API
  39. #endif
  40.  
  41.  
  42. #if defined(_WIN32)//  All this taken from glut.h
  43. #  ifndef APIENTRY
  44. #   define GLUT_APIENTRY_DEFINED
  45. #   if (_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED) || defined(__BORLANDC__) || defined(__LCC__)
  46. #    define APIENTRY    __stdcall
  47. #   else
  48. #    define APIENTRY
  49. #   endif
  50. #  endif
  51. /* XXX This is from Win32's <winnt.h> */
  52. #  ifndef CALLBACK
  53. #   if (defined(_M_MRX000) || defined(_M_IX86) || defined(_M_ALPHA) || defined(_M_PPC)) && !defined(MIDL_PASS) || defined(__LCC__)
  54. #    define CALLBACK __stdcall
  55. #   else
  56. #    define CALLBACK
  57. #   endif
  58. #  endif
  59. /* XXX Hack for lcc compiler.  It doesn't support __declspec(dllimport), just __stdcall. */
  60. #  if defined( __LCC__ )
  61. #   undef WINGDIAPI
  62. #   define WINGDIAPI __stdcall
  63. #  else
  64. /* XXX This is from Win32's <wingdi.h> and <winnt.h> */
  65. #   ifndef WINGDIAPI
  66. #    define GLUT_WINGDIAPI_DEFINED
  67. #    define WINGDIAPI __declspec(dllimport)
  68. #   endif
  69. #  endif
  70. /* XXX This is from Win32's <ctype.h> */
  71. #  ifndef _WCHAR_T_DEFINED
  72. typedef unsigned short wchar_t;
  73. #   define _WCHAR_T_DEFINED
  74. #  endif
  75. # endif //win32 end glut.h stuff
  76.  
  77. /* XXX Hack for finding headers in Apple's OpenGL framework. */
  78. #if defined( __APPLE__ )
  79. #include <OpenGL/gl.h>
  80. #include <OpenGL/glu.h>
  81. #else /* __APPLE__ */
  82. #include <GL/gl.h>
  83. #include <GL/glu.h>
  84. #endif /* __APPLE__ */
  85. #include <list>
  86. #include <set>
  87.  
  88.  
  89. #include "..\..\CEGUIBase.h"
  90. #include "..\..\CEGUIRenderer.h"
  91. #include "..\..\CEGUITexture.h"
  92.  
  93.  
  94. #if defined(_WIN32)
  95. #  if defined(_DEBUG)
  96. #     pragma comment(lib, "CEGUIBase_d.lib")
  97. #  else
  98. #     pragma comment(lib, "CEGUIBase.lib")
  99. #  endif
  100. #endif
  101.  
  102. #if defined(_MSC_VER)
  103. #    pragma warning(push)
  104. #    pragma warning(disable : 4251)
  105. #endif
  106.  
  107. #define OGLRENDERER_VBUFF_CAPACITY    4096
  108.  
  109.  
  110. // Start of CEGUI namespace section
  111. namespace CEGUI
  112. {
  113. /*************************************************************************
  114.     Forward refs
  115. *************************************************************************/
  116. class OpenGLTexture;
  117.  
  118. /*!
  119. \brief
  120. Renderer class to interface with OpenGL
  121. */
  122. class OPENGL_GUIRENDERER_API OpenGLRenderer : public Renderer
  123. {
  124. public:
  125.     /*!
  126.     \brief
  127.         Constructor for OpenGL Renderer object
  128.  
  129.     \param max_quads
  130.         obsolete.  Set to 0.
  131.     */
  132.     OpenGLRenderer(uint max_quads);
  133.     OpenGLRenderer(uint max_quads,int width, int height);
  134.  
  135.  
  136.     /*!
  137.     \brief
  138.         Destructor for OpenGLRenderer objects
  139.     */
  140.     virtual ~OpenGLRenderer(void);
  141.  
  142.     // add's a quad to the list to be rendered
  143.     virtual    void    addQuad(const Rect& dest_rect, float z, const Texture* tex, const Rect& texture_rect, const ColourRect& colours, QuadSplitMode quad_split_mode);
  144.  
  145.     // perform final rendering for all queued renderable quads.
  146.     virtual    void    doRender(void);
  147.  
  148.     // clear the queue
  149.     virtual    void    clearRenderList(void);
  150.  
  151.  
  152.     /*!
  153.     \brief
  154.         Enable or disable the queuing of quads from this point on.
  155.  
  156.         This only affects queuing.  If queuing is turned off, any calls to addQuad will cause the quad to be rendered directly.  Note that
  157.         disabling queuing will not cause currently queued quads to be rendered, nor is the queue cleared - at any time the queue can still
  158.         be drawn by calling doRender, and the list can be cleared by calling clearRenderList.  Re-enabling the queue causes subsequent quads
  159.         to be added as if queuing had never been disabled.
  160.  
  161.     \param setting
  162.         true to enable queuing, or false to disable queuing (see notes above).
  163.  
  164.     \return
  165.         Nothing
  166.     */
  167.     virtual void    setQueueingEnabled(bool setting)        {d_queueing = setting;}
  168.  
  169.  
  170.     // create an empty texture
  171.     virtual    Texture*    createTexture(void);
  172.  
  173.     // create a texture and load it with the specified file.
  174.     virtual    Texture*    createTexture(const String& filename, const String& resourceGroup);
  175.  
  176.     // create a texture and set it to the specified size
  177.     virtual    Texture*    createTexture(float size);
  178.  
  179.     // destroy the given texture
  180.     virtual    void        destroyTexture(Texture* texture);
  181.  
  182.     // destroy all textures still active
  183.     virtual void        destroyAllTextures(void);
  184.  
  185.     /*!
  186.     \brief
  187.         Return whether queuing is enabled.
  188.  
  189.     \return
  190.         true if queuing is enabled, false if queuing is disabled.
  191.     */
  192.     virtual bool    isQueueingEnabled(void) const    {return d_queueing;}
  193.  
  194.  
  195.     /*!
  196.     \brief
  197.         Return the current width of the display in pixels
  198.  
  199.     \return
  200.         float value equal to the current width of the display in pixels.
  201.     */
  202.     virtual float    getWidth(void) const        {return d_display_area.getWidth();}
  203.  
  204.  
  205.     /*!
  206.     \brief
  207.         Return the current height of the display in pixels
  208.  
  209.     \return
  210.         float value equal to the current height of the display in pixels.
  211.     */
  212.     virtual float    getHeight(void) const        {return d_display_area.getHeight();}
  213.  
  214.  
  215.     /*!
  216.     \brief
  217.         Return the size of the display in pixels
  218.  
  219.     \return
  220.         Size object describing the dimensions of the current display.
  221.     */
  222.     virtual Size    getSize(void) const            {return d_display_area.getSize();}
  223.  
  224.  
  225.     /*!
  226.     \brief
  227.         Return a Rect describing the screen
  228.  
  229.     \return
  230.         A Rect object that describes the screen area.  Typically, the top-left values are always 0, and the size of the area described is
  231.         equal to the screen resolution.
  232.     */
  233.     virtual Rect    getRect(void) const            {return d_display_area;}
  234.  
  235.  
  236.     /*!
  237.     \brief
  238.         Return the maximum texture size available
  239.  
  240.     \return
  241.         Size of the maximum supported texture in pixels (textures are always assumed to be square)
  242.     */
  243.     virtual    uint    getMaxTextureSize(void) const        {return d_maxTextureSize;}
  244.  
  245.  
  246.     /*!
  247.     \brief
  248.         Return the horizontal display resolution dpi
  249.  
  250.     \return
  251.         horizontal resolution of the display in dpi.
  252.     */
  253.     virtual    uint    getHorzScreenDPI(void) const    {return 96;}
  254.  
  255.  
  256.     /*!
  257.     \brief
  258.         Return the vertical display resolution dpi
  259.  
  260.     \return
  261.         vertical resolution of the display in dpi.
  262.     */
  263.     virtual    uint    getVertScreenDPI(void) const    {return 96;}
  264.  
  265.  
  266.     /*!
  267.     \brief
  268.         Set the size of the display in pixels.
  269.  
  270.         If your viewport size changes, you can call this function with the new size
  271.         in pixels to update the rendering area.
  272.  
  273.     \note
  274.         This method will cause the EventDisplaySizeChanged event to fire if the
  275.         display size has changed.
  276.  
  277.     \param sz
  278.         Size object describing the size of the display.
  279.  
  280.     \return
  281.         Nothing.
  282.     */
  283.     void    setDisplaySize(const Size& sz);
  284.  
  285.  
  286. private:
  287.     /************************************************************************
  288.         Implementation Constants
  289.     ************************************************************************/
  290.     const static int            VERTEX_PER_QUAD;                            //!< number of vertices per quad
  291.     const static int            VERTEX_PER_TRIANGLE;                        //!< number of vertices for a triangle
  292.     const static int            VERTEXBUFFER_CAPACITY;                        //!< capacity of the allocated vertex buffer
  293.     
  294.     /*************************************************************************
  295.         Implementation Structs & classes
  296.     *************************************************************************/
  297.     struct MyQuad
  298.     {
  299.         float tex[2];
  300.         long color;
  301.         float vertex[3];
  302.     };
  303.  
  304.     /*!
  305.     \brief
  306.         structure holding details about a quad to be drawn
  307.     */
  308.     struct QuadInfo
  309.     {
  310.         GLuint        texid;  
  311.         Rect        position;
  312.         float        z;
  313.         Rect        texPosition;
  314.         long        topLeftCol;
  315.         long        topRightCol;
  316.         long        bottomLeftCol;
  317.         long        bottomRightCol;
  318.  
  319.         QuadSplitMode   splitMode;
  320.  
  321.         bool operator<(const QuadInfo& other) const
  322.         {
  323.             // this is intentionally reversed.
  324.             return z > other.z;
  325.         }
  326.  
  327.     };
  328.  
  329.  
  330.     /*************************************************************************
  331.         Implementation Methods
  332.     *************************************************************************/
  333.     // setup states etc
  334.     void    initPerFrameStates(void);
  335.  
  336.     // restore states
  337.     void  exitPerFrameStates(void);
  338.  
  339.     // render whatever is in the vertex buffer
  340.     void    renderVBuffer(void);
  341.  
  342.     // sort quads list according to texture
  343.     void    sortQuads(void);
  344.  
  345.     // render a quad directly to the display
  346.     void    renderQuadDirect(const Rect& dest_rect, float z, const Texture* tex, const Rect& texture_rect, const ColourRect& colours, QuadSplitMode quad_split_mode);
  347.  
  348.     // convert colour value to whatever the OpenGL system is expecting.
  349.     long    colourToOGL(const colour& col) const;
  350.  
  351.     // set the module ID string
  352.     void setModuleIdentifierString();
  353.  
  354.  
  355.     /*************************************************************************
  356.         Implementation Data
  357.     *************************************************************************/
  358.     typedef std::multiset<QuadInfo>        QuadList;
  359.     QuadList d_quadlist;
  360.  
  361.     Rect        d_display_area;
  362.  
  363.     MyQuad        myBuff[OGLRENDERER_VBUFF_CAPACITY];
  364.  
  365.     bool        d_queueing;            //!< setting for queuing control.
  366.     uint        d_currTexture;        //!< Currently bound texture.
  367.     int            d_bufferPos;        //!< index into buffer where next vertex should be put.
  368.     bool        d_sorted;            //!< true when data in quad list is sorted.
  369.  
  370.     std::list<OpenGLTexture*>    d_texturelist;        //!< List used to track textures.
  371.     GLint       d_maxTextureSize;        //!< Holds maximum supported texture size (in pixels).
  372. };
  373.  
  374. } // End of  CEGUI namespace section
  375.  
  376. #if defined(_MSC_VER)
  377. #    pragma warning(pop)
  378. #endif
  379.  
  380. #endif    // end of guard _openglrenderer_h_
  381.